import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.net.URL;
import java.text.MessageFormat;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

/**
 * 类描述：
 *
 * @创建人：john.zhou@chinablackhawk.com
 * @创建时间：2018/10/8 14:47
 * @修改人：
 * @修改时间：
 * @修改备注：
 * @版权：Copyright© 2015 - 2018 神州黑鹰（上海）信息科技有限公司版权所有。
 */
@Service
public class WechatSendMsgServiceImpl {

    private static final Logger LOGGER = LoggerFactory.getLogger(WechatSendMsgServiceImpl.class);

    private static final String SEND_MSG_URL = "http://open.campus.qq.com/api/open/sendMsg?timestamp={0}&sign={1}&openAppID={2}&objectid={3}&objType={4}" +
            "&data[msgtype]={5}&data[wxuserid]={6}&data[content]={7}";

    private static final String SEND_MSG_AND_DEPART_URL = "http://open.campus.qq.com/api/open/sendMsg?timestamp={0}&sign={1}&openAppID={2}&objectid={3}&objType={4}" +
            "&data[msgtype]={5}&data[wxuserid]={6}&data[wxdepartid]={7}&data[content]={8}";

    private static final String GET_USER_INFO = "https://open.campus.qq.com/api/open/getUserInfo?timestamp={0}&openAppID={1}&userid={2}&objType={3}&objectid={4}&sign={5}";



    public static void main(String[] args) throws Exception {
        // 第一步，实例化一个document对象
        Document document = new Document();
        // 第二步，设置要到出的路径
        FileOutputStream out = new FileOutputStream("D:\\workbook111.pdf");
        //如果是浏览器通过request请求需要在浏览器中输出则使用下面方式
        //OutputStream out = response.getOutputStream();
        // 第三步,设置字符
        BaseFont bfChinese = BaseFont.createFont("C:/windows/fonts/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font fontZH = new Font(bfChinese, 12.0F, 0);
        // 第四步，将pdf文件输出到磁盘
        PdfWriter writer = PdfWriter.getInstance(document, out);
        // 第五步，打开生成的pdf文件
        document.open();
        // 第六步,设置内容
        String title = "标题";
        document.add(new Paragraph(new Chunk(title, fontZH).setLocalDestination(title)));
        document.add(new Paragraph("\n"));
        // 创建table,注意这里的2是两列的意思,下面通过table.addCell添加的时候必须添加整行内容的所有列
        PdfPTable table = new PdfPTable(6);
        table.setWidthPercentage(100.0F);
        table.setHeaderRows(1);
        table.getDefaultCell().setHorizontalAlignment(1);

        PdfPCell cell;
        table.addCell(new Paragraph("序号", fontZH));
        table.addCell(new Paragraph("结果", fontZH));
        cell = new PdfPCell(new Paragraph("图片", fontZH));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        cell.setColspan(4);
        table.addCell(cell);

        table.addCell(new Paragraph("1", fontZH));
        table.addCell(new Paragraph("成功", fontZH));
//        Image gif = Image.getInstance("C:\\Users\\heimo\\Desktop\\face.jpg");
        Image gif  = Image.getInstance(new URL("http://pic29.nipic.com/20130511/9252150_174018365301_2.jpg"));
        gif.scaleAbsolute(40f,40f);
        cell = new PdfPCell();
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        cell.setColspan(4);
        cell.setFixedHeight(40);
        cell.setImage(gif);
        table.addCell(cell);

        cell = new PdfPCell(new Paragraph("2", fontZH));
        cell.setFixedHeight(80);
        cell.setRowspan(2);
        table.addCell(cell);
        table.addCell(new Paragraph("成功", fontZH));
//        Image gif = Image.getInstance("C:\\Users\\heimo\\Desktop\\face.jpg");
        gif  = Image.getInstance(new URL("http://pic29.nipic.com/20130511/9252150_174018365301_2.jpg"));
        gif.scaleAbsolute(40f,40f);
        cell = new PdfPCell();
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        cell.setColspan(4);
        cell.setFixedHeight(40);
        cell.setImage(gif);
        table.addCell(cell);

        table.addCell(new Paragraph("成功", fontZH));
//        gif = Image.getInstance("C:\\Users\\heimo\\Desktop\\back.jpg");
        gif  = Image.getInstance(new URL("http://pic29.nipic.com/20130511/9252150_174018365301_2.jpg"));
        gif.scaleAbsolute(40f,40f);
        cell = new PdfPCell();
        cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中
        cell.setColspan(4);
        cell.setFixedHeight(40);
        cell.setImage(gif);
        table.addCell(cell);


        document.add(table);
        document.add(new Paragraph("\n"));
        // 第七步，关闭document
        document.close();

        System.out.println("导出pdf成功~");

    }

}
